//TODO: rename Collections
//db.TemplateCriteria.renameCollection("OldTemplateCriteria")

//TODO: Copy PIIndicator collection from ClientDW_A to ClientPlatform
//TODO: After running the script delete the collection from ClientPlatform 

function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
    });
    return UUID(uuid);
};

var templateCriteria = db.OldTemplateCriteria.find();
var count = db.OldTemplateCriteria.count();

while (templateCriteria.hasNext())
{
	//add Order to OldTemplateCriteria
    var currentItem = templateCriteria.next();
    currentItem.Order = count;
    db.OldTemplateCriteria.save(currentItem);
    count--; 
   
    //Create CriteriaLevel3
    var criteriaLevel3Item = 
    {
		_id: generateUUID(),
        Name: currentItem.Name,
        ShortName : "",
        Order: currentItem.Order,
        CriteriaLevel2Id: currentItem.CriteriaLevel2Id
    }
    db.CriteriaLevel3.save(criteriaLevel3Item);
	//print(criteriaLevel3Item);
    
    //Create new TemplateCriteria
    switch (currentItem.CriteriaType)
	{
		case 2:
			//add TemplateCriteria for AnswerCategory
			var templateCriteriaForAnswerCategory =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Answer Category",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "AnswerCategory",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: true
			}
			db.TemplateCriteria.save(templateCriteriaForAnswerCategory);
			print("templateCriteriaForAnswerCategory: ");
			print(templateCriteriaForAnswerCategory);
			
			//add TemplateCriteria for Summary
			var templateCriteriaForSummary =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Summary",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "AnswerCategoryText",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: false
			}
			db.TemplateCriteria.save(templateCriteriaForSummary);
			print("templateCriteriaForSummary: ");
			print(templateCriteriaForSummary);
			break;
			
		//if I don't include the CriteriaType 4 in new TemplateCriteria I will lose the _id of CriteriaLevel3
		//this case will be included in the default of the switch
		case 4:
		    var categories = db.PIIndicator.findOne({Symbol: currentItem.PISymbol}).Categories;
			for (var i = 0; i < categories.length; i++)
			{
				var category = categories[i];
				//print(category);
				var tempCriteriaForCategoryInvolvement = 
				{
					_id: generateUUID(),
					Name: currentItem.Name + " " + categories[i].CategoryOfInvolvement + "-Category of Involvement",
					CriteriaEntity: "ScreeningData",
					CriteriaEntityField: currentItem.PISymbol + " " + categories[i].Symbol + " Category of Involvement",
					CriteriaType: currentItem.CriteriaType,
					CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
					CriteriaLevel3Id: criteriaLevel3Item._id,
					IsScreenable: false
				}
				db.TemplateCriteria.save(tempCriteriaForCategoryInvolvement);
				print("tempCriteriaForCategoryInvolvement: ");
				print(tempCriteriaForCategoryInvolvement);
				
				/* var tempCriteriaForStandardAnalysis =
				{
					_id: generateUUID(),
					Name: currentItem.Name + " " + categories[i].CategoryOfInvolvement + "-Standard Analysis",
					CriteriaEntity: "ScreeningData",
					CriteriaEntityField: currentItem.PISymbol + " " + categories[i].Symbol + " Standard Analysis",
					CriteriaType: currentItem.CriteriaType,
					CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
					CriteriaLevel3Id: criteriaLevel3Item._id,
					IsScreenable: false
				}
				db.TemplateCriteria.save(tempCriteriaForStandardAnalysis);
				print("tempCriteriaForStandardAnalysis: ");
				print(tempCriteriaForStandardAnalysis); */
				
				if (categories[i].RevenueRangeYesNo == true)
				{
					var tempCriteriaForLevelInvolvement =
					{
						_id: generateUUID(),
						Name: currentItem.Name + " " + categories[i].CategoryOfInvolvement  + "-Level of Involvement",
						CriteriaEntity: "ScreeningData",
						CriteriaEntityField: currentItem.PISymbol + " " + categories[i].Symbol + " Level of Involvement",
						CriteriaType: currentItem.CriteriaType,
						CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
						CriteriaLevel3Id: criteriaLevel3Item._id,
						IsScreenable: true
					}
					db.TemplateCriteria.save(tempCriteriaForLevelInvolvement);
					print("tempCriteriaForLevelInvolvement: ");
					print(tempCriteriaForLevelInvolvement);
				}
				
				if (categories[i].OwnershipYesNo == true)
				{
					var tempCriteriaForRangeOwnership =
					{
						_id: generateUUID(),
						Name: currentItem.Name + " " + categories[i].CategoryOfInvolvement + "-Range Ownership",
						CriteriaEntity: "ScreeningData",
						CriteriaEntityField: currentItem.PISymbol + " " + categories[i].Symbol + " Range Ownership",
						CriteriaType: currentItem.CriteriaType,
						CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
						CriteriaLevel3Id: criteriaLevel3Item._id,
						IsScreenable: true
					}
					db.TemplateCriteria.save(tempCriteriaForRangeOwnership);
					print("tempCriteriaForRangeOwnership: ");
					print(tempCriteriaForRangeOwnership);
				}
					
				if ((categories[i].OwnershipYesNo == false) && (categories[i].RevenueRangeYesNo == false))
				{
					var tempCriteriaForRangeOwnership =
					{
						_id: generateUUID(),
						Name: currentItem.Name + " " + categories[i].CategoryOfInvolvement + "-Level of Involvement",
						CriteriaEntity: "ScreeningData",
						CriteriaEntityField: currentItem.PISymbol + " " + categories[i].Symbol + " Level of Involvement",
						CriteriaType: currentItem.CriteriaType,
						CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
						CriteriaLevel3Id: criteriaLevel3Item._id,
						IsScreenable: true
					}
					db.TemplateCriteria.save(tempCriteriaForRangeOwnership);
					print("tempCriteriaForRangeOwnership: ");
					print(tempCriteriaForRangeOwnership);
				}
			}
			break;
			
		case 5:   
			//add TemplateCriteria for Score
			var templateCriteriaForScore =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Raw Score",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "SelectedAnswer.Score",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: true
			}
			db.TemplateCriteria.save(templateCriteriaForScore);
			print("templateCriteriaForScore: ");
			print(templateCriteriaForScore);
			
			//add TemplateCriteria for Weight
			var templateCriteriaForWeight =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Weight",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "Weight",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: false
			}
			db.TemplateCriteria.save(templateCriteriaForWeight);
			print("templateCriteriaForWeight: ");
			print(templateCriteriaForWeight);
			
			//add TemplateCriteria for WeightedScore
			var templateCriteriaForWeightedScore =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Weighted Score",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "WeightedScore",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: false
			}
			db.TemplateCriteria.save(templateCriteriaForWeightedScore);
			print("templateCriteriaForWeightedScore: ");
			print(templateCriteriaForWeightedScore);
			
			//add TemplateCriteria for Summary
			var templateCriteriaForAnswerCategory =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Answer Category",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "SelectedAnswer.Text",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: false
			}
		    db.TemplateCriteria.save(templateCriteriaForAnswerCategory);
			print("templateCriteriaForAnswerCategory: ");
			print(templateCriteriaForAnswerCategory);
			
			//add TemplateCriteria for Summary
			/* var templateCriteriaForComments =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Comments",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "SelectedAnswer.Text",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: false
			}
			db.TemplateCriteria.save(templateCriteriaForComments);
			print("templateCriteriaForSummary: ");
			print(templateCriteriaForComments); */
			break;
			
		case 7:
			var templateCriteriaStatus =
			    {
			        _id: generateUUID(),
			        Name: "Overall Global Compact Compliance Status",
			        CriteriaEntity: "ScreeningData",
			        CriteriaEntityField: "Overall Global Compact Compliance Status",
			        CriteriaType: currentItem.CriteriaType,
			        CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
			        CriteriaLevel3Id: criteriaLevel3Item._id,
			        IsScreenable: true
			    }
			db.TemplateCriteria.save(templateCriteriaStatus);
			print("templateCriteriaStatus: ");
			print(templateCriteriaStatus);
		
		    //var wTemplateCriteriaStatus =
			//    {
			//        _id: generateUUID(),
			//        Name: "Watchlist",
			//        CriteriaEntity: "ScreeningData",
			//        CriteriaEntityField: "Overall Global Compact Compliance Status",
			//        CriteriaType: currentItem.CriteriaType,
			//        CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
			//        CriteriaLevel3Id: criteriaLevel3Item._id,
			//        IsScreenable: true
			//    }
			//db.TemplateCriteria.save(wTemplateCriteriaStatus);
			//print("wTemplateCriteriaStatus: ");
			//print(wTemplateCriteriaStatus);

			//var nTemplateCriteriaStatus =
			//    {
			//        _id: generateUUID(),
			//        Name: "Non-Compliant",
			//        CriteriaEntity: "ScreeningData",
			//        CriteriaEntityField: "Overall Global Compact Compliance Status",
			//        CriteriaType: currentItem.CriteriaType,
			//        CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
			//        CriteriaLevel3Id: criteriaLevel3Item._id,
			//        IsScreenable: true
			//    }
			//db.TemplateCriteria.save(nTemplateCriteriaStatus);
			//print("nTemplateCriteriaStatus: ");
			//print(nTemplateCriteriaStatus);

			//var cTemplateCriteriaStatus =
			//    {
			//        _id: generateUUID(),
			//        Name: "Compliant",
			//        CriteriaEntity: "ScreeningData",
			//        CriteriaEntityField: "Overall Global Compact Compliance Status",
			//        CriteriaType: currentItem.CriteriaType,
			//        CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
			//        CriteriaLevel3Id: criteriaLevel3Item._id,
			//        IsScreenable: true
			//    }
			//db.TemplateCriteria.save(cTemplateCriteriaStatus);
			//print("cTemplateCriteriaStatus: ");
			//print(cTemplateCriteriaStatus);
			
			var templateCriteriaBreached =
            {
                _id: generateUUID(),
                Name: "Global Compact Compliance-Principle Breached", 
                CriteriaEntity: "ScreeningData",
                CriteriaEntityField: "Global Compact Compliance Principle Breached",
                CriteriaType: currentItem.CriteriaType,
                CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
                CriteriaLevel3Id: criteriaLevel3Item._id,
                IsScreenable: false
            }
            db.TemplateCriteria.save(templateCriteriaBreached);
            print("templateCriteriaBreached: ");
            print(templateCriteriaBreached);
			
			//var templateCriteriaSummary =
            //{
            //    _id: generateUUID(),
            //    Name: "Global Compact Compliance-Summary", 
            //    CriteriaEntity: "ScreeningData",
            //    CriteriaEntityField: "Global Compact Compliance Summary",
            //    CriteriaType: currentItem.CriteriaType,
            //     CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
            //     CriteriaLevel3Id: criteriaLevel3Item._id,
            //     IsScreenable: false
            //}
            //db.TemplateCriteria.save(templateCriteriaSummary);
            //print("templateCriteriaSummary: ");
            //print(templateCriteriaSummary);
			
			//var wTemplateCriteriaBreached =
            //{
            //    _id: generateUUID(),
            //    Name: "Watchlist Global Compact Compliance Principle Breached", 
            //    CriteriaEntity: "ScreeningData",
            //    CriteriaEntityField: "Global Compact Compliance Principle Breached",
            //    CriteriaType: currentItem.CriteriaType,
            //    CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
            //    CriteriaLevel3Id: criteriaLevel3Item._id,
            //    IsScreenable: false
            //}
            //db.TemplateCriteria.save(wTemplateCriteriaBreached);
            //print("wTemplateCriteriaBreached: ");
            //print(wTemplateCriteriaBreached);
			
            //var nTemplateCriteriaBreached =
            //{
            //    _id: generateUUID(),
            //    Name: "Non-Compliant Global Compact Compliance Principle Breached", 
            //    CriteriaEntity: "ScreeningData",
            //    CriteriaEntityField: "Global Compact Compliance Principle Breached",
            //    CriteriaType: currentItem.CriteriaType,
            //    CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
            //    CriteriaLevel3Id: criteriaLevel3Item._id,
            //    IsScreenable: false
            //}
            //db.TemplateCriteria.save(nTemplateCriteriaBreached);
            //print("nTemplateCriteriaBreached: ");
            //print(nTemplateCriteriaBreached);
			
            //var cTemplateCriteriaBreached =
            //{
            //    _id: generateUUID(),
            //    Name: "Compliant Global Compact Compliance Principle Breached",
            //    CriteriaEntity: "ScreeningData",
            //    CriteriaEntityField: "Global Compact Compliance Principle Breached",
            //    CriteriaType: currentItem.CriteriaType,
            //    CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
            //    CriteriaLevel3Id: criteriaLevel3Item._id,
            //    IsScreenable: false
            //}
            //db.TemplateCriteria.save(cTemplateCriteriaBreached);
            //print("cTemplateCriteriaBreached: ");
            //print(cTemplateCriteriaBreached);
			
            //var wTemplateCriteriaSummary =
            //{
            //    _id: generateUUID(),
            //    Name: "Watchlist Global Compact Compliance Summary", 
            //    CriteriaEntity: "ScreeningData",
            //    CriteriaEntityField: "Global Compact Compliance Summary",
            //    CriteriaType: currentItem.CriteriaType,
            //    CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
            //    CriteriaLevel3Id: criteriaLevel3Item._id,
            //    IsScreenable: false
            //}
            //db.TemplateCriteria.save(wTemplateCriteriaSummary);
            //print("wTemplateCriteriaSummary: ");
            //print(wTemplateCriteriaSummary);
			
            //var nTemplateCriteriaSummary =
            //{
            //    _id: generateUUID(),
            //    Name: "Non-Compliant Global Compact Compliance Summary", 
            //    CriteriaEntity: "ScreeningData",
            //    CriteriaEntityField: "Global Compact Compliance Summary",
            //    CriteriaType: currentItem.CriteriaType,
            //    CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
            //    CriteriaLevel3Id: criteriaLevel3Item._id,
            //    IsScreenable: false
            //}
            //db.TemplateCriteria.save(nTemplateCriteriaSummary);
            //print("nTemplateCriteriaSummary: ");
            //print(nTemplateCriteriaSummary);
			
            //var cTemplateCriteriaSummary =
            //{
            //    _id: generateUUID(),
            //    Name: "Compliant Global Compact Compliance Summary", 
            //    CriteriaEntity: "ScreeningData",
            //    CriteriaEntityField: "Global Compact Compliance Summary",
            //    CriteriaType: currentItem.CriteriaType,
            //    CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
            //    CriteriaLevel3Id: criteriaLevel3Item._id,
            //    IsScreenable: false
            //}
            //db.TemplateCriteria.save(cTemplateCriteriaSummary);
            //print("cTemplateCriteriaSummary: ");
            //print(cTemplateCriteriaSummary);
			
			break;
			
		 case 8:	 
			//add TemplateCriteria for AnswerCategory
			var templateCriteriaForAnswerCategory =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Answer Category",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "AnswerCategory",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: true
			}
			db.TemplateCriteria.save(templateCriteriaForAnswerCategory);
			print("templateCriteriaForAnswerCategory: ");
			print(templateCriteriaForAnswerCategory);
			
			//add TemplateCriteria for Summary
	/* 		var templateCriteriaForSummary =
			{
				_id: generateUUID(),
				Name: currentItem.TemplateIndicatorNumber + " " + currentItem.Name + "-Answer Category",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "SelectedAnswer.Text",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: false
			}
			db.TemplateCriteria.save(templateCriteriaForSummary);
			print("templateCriteriaForSummary: ");
			print(templateCriteriaForSummary); */
			
			//add TemplateCriteria for Weight
			var templateCriteriaForWeight =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Weight",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "Weight",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: false
			}
			db.TemplateCriteria.save(templateCriteriaForWeight);
			print("templateCriteriaForWeight: ");
			print(templateCriteriaForWeight);
			
			//add TemplateCriteria for WeightedScore
			var templateCriteriaForWeightedScore =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Weighted Score",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "WeightedScore",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: false
			}
			db.TemplateCriteria.save(templateCriteriaForWeightedScore);
			print("templateCriteriaForWeightedScore: ");
			print(templateCriteriaForWeightedScore);
			
			// add TemplateCriteria for Assessment
			var templateCriteriaForAssessment =
			{
				_id: generateUUID(),
				Name: currentItem.Name + "-Assessment",
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: "assessment",
				CriteriaType: currentItem.CriteriaType,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id: criteriaLevel3Item._id,
				IsScreenable: false
			}
			db.TemplateCriteria.save(templateCriteriaForAssessment);
			print("templateCriteriaForAssessment: ");
			print(templateCriteriaForAssessment);
		    break;
			
			
		 default:
			//used for maping CriteriaType: 1,3,6
			var templateCriteriaItem =
			{
				_id: generateUUID(),
				Name: currentItem.Name,
				CriteriaEntity: currentItem.CriteriaEntity,
				CriteriaEntityField: currentItem.CriteriaEntityField,
				CriteriaLevel2Id: currentItem.CriteriaLevel2Id,
				CriteriaLevel3Id:  criteriaLevel3Item._id,
				CriteriaType: currentItem.CriteriaType,
				_id: currentItem._id,
				IsScreenable: true
			}
			db.TemplateCriteria.save(templateCriteriaItem);
			print("templateCriteriaItem: ");
			print(templateCriteriaItem);
			break;
	}
}

/*
db.TemplateCriteria.find({CriteriaType:1}).count()
db.OldTemplateCriteria.find({CriteriaType:1}).count()
db.TemplateCriteria.find({CriteriaType:1})
db.OldTemplateCriteria.find({CriteriaType:1})

db.TemplateCriteria.find({CriteriaType:2}).count()
db.OldTemplateCriteria.find({CriteriaType:2}).count()
db.TemplateCriteria.find({CriteriaType:2})
db.OldTemplateCriteria.find({CriteriaType:2})

db.TemplateCriteria.find({CriteriaType:3}).count()
db.OldTemplateCriteria.find({CriteriaType:3}).count()
db.TemplateCriteria.find({CriteriaType:3})
db.OldTemplateCriteria.find({CriteriaType:3})

db.TemplateCriteria.find({CriteriaType:4}).count()
db.OldTemplateCriteria.find({CriteriaType:4}).count()
db.TemplateCriteria.find({CriteriaType:4})
db.OldTemplateCriteria.find({CriteriaType:4})

db.TemplateCriteria.find({CriteriaType:5}).count()
db.OldTemplateCriteria.find({CriteriaType:5}).count()
db.TemplateCriteria.find({CriteriaType:5})
db.OldTemplateCriteria.find({CriteriaType:5})

db.TemplateCriteria.find({CriteriaType:6}).count()
db.OldTemplateCriteria.find({CriteriaType:6}).count()
db.TemplateCriteria.find({CriteriaType:6})
db.OldTemplateCriteria.find({CriteriaType:6})

db.TemplateCriteria.find({CriteriaType:7}).count()
db.OldTemplateCriteria.find({CriteriaType:7}).count()
db.TemplateCriteria.find({CriteriaType:7})
db.OldTemplateCriteria.find({CriteriaType:7})

db.TemplateCriteria.find({CriteriaType:8}).count()
db.OldTemplateCriteria.find({CriteriaType:8}).count()
db.TemplateCriteria.find({CriteriaType:8})
db.OldTemplateCriteria.find({CriteriaType:8})
*/


